package 二叉树相关问题.层次遍历;


import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class 从上到下打印二叉树1 {
    public int[] levelOrder(TreeNode root) {
        if (root==null)
            return new int[0];
        List<Integer> list = new LinkedList<>();
        Queue<TreeNode> q=new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()){
            TreeNode poll = q.poll();
            if (poll==null)
                continue;
            list.add(poll.val);
            q.offer(root.left);
            q.offer(root.right);
        }
        int[] res = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            res[i]= list.get(i);
        }
        return res;
    }
}
